package org.doofer.test

class MockTypedObject {

    /**
     * Constructor
     */
    MockTypedObject() {}

    def attribs = [:]

    def singleGetters = ['getBoolean', 'getDouble', 'getId', 'getInt', 'getString', 'getTime']
    def repeatingGetters = ['getRepeatingBoolean', 'getRepeatingDouble', 'getRepeatingId',
                            'getRepeatingInt', 'getRepeatingString', 'getRepeatingTime']
    def singleSetters = ['setBoolean', 'setDouble', 'setId', 'setInt', 'setString', 'setTime']
    def repeatingSetters = ['setRepeatingBoolean', 'setRepeatingDouble', 'setRepeatingId',
                            'setRepeatingInt', 'setRepeatingString', 'setRepeatingTime']

    def add(MockAttrib attrib) {
        attribs."$attrib.name" = attrib
    }

    def getValueCount(attribName) {
        //println "invoked getValueCount() on mock"
        def result = attribs."$attribName".getValueCount()
        //println "result = $result"
        result
    }

    def getAttrDataType(attribName) {
        //println "invoked getAttrDataType() on mock"
        def result = attribs."$attribName".type
        //println "result = $result"
        result
    }

    def isAttrRepeating(attribName) {
        //println "invoked isAttrRepeating() on mock"
        def result = attribs."$attribName".isRepeating
        //println "result = $result"
        result
    }

    def truncate(attribName, index) {
        //println "invoked truncate() on mock"
        attribs."$attribName".truncate(index)
    }

    /**
     * Convenience method for getting value back without
     * going through usual getBoolean/getDouble/etc..
     */
    def getValue(attribName) {
        attribs."$attribName".@value // the @ synbol instructs retrieving the actual rather than go thru the getter
    }

    def methodMissing(String methodName, args) {
        //println "invoked $methodName() on mock"
        def attribName = args[0]
        def index
        def newValue

        if(singleGetters.contains(methodName)) {
            return attribs."$attribName".value
        } else if(repeatingGetters.contains(methodName)) {
            index = args[1]
            return attribs."$attribName".getValue(index)
        } else if(singleSetters.contains(methodName)) {
            newValue = args[1]
            attribs."$attribName".value = newValue
        } else if(repeatingSetters.contains(methodName)) {
            index = args[1]
            newValue = args[2]
            attribs."$attribName".setValue(newValue, index)
        } else {
            throw new MissingMethodException(name, MockTypedObject.class, args)
        }
    }

}